package com.talent.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.talent.page.Page;
import com.talent.pojo.Course;
import com.talent.pojo.Depart;
import com.talent.pojo.Major;
import com.talent.pojo.Schedule;
import com.talent.pojo.TrainDetail;
import com.talent.pojo.User;
import com.talent.service.CourseService;
import com.talent.service.DepartService;
import com.talent.service.MajorService;
import com.talent.service.OpenStatusService;
import com.talent.service.ScheduleService;
import com.talent.service.TrainDetailService;
import com.talent.service.TrainPlanService;
import com.talent.service.UserService;
import com.talent.vo.Result;
import com.talent.vo.TrainDetailVo;
import com.talent.vo.TrainPlanVo;
/**
 * 培养方案
 * @author mi
 *
 */
@Controller
@RequestMapping("/trainPlan")
public class TrainPlanController extends BaseController{
	@Resource
	private TrainPlanService trainPlanService;
	@Resource
	private TrainDetailService trainDetailService;
	@Resource
	private DepartService departService;
	@Resource
	private MajorService majorService;
	@Resource
	private ScheduleService scheduleService;
	@Resource
	private OpenStatusService openStatusService;
	@Resource
	private CourseService courseService;
	@Resource
	private UserService userService;
	 @RequestMapping(value = "/manager",method = RequestMethod.GET)
	 public ModelAndView trainPlanUI(TrainPlanVo trainPlan,Integer currentPage, ModelAndView mav){
	        try {
	        	User user = getCurrentUser(getSession());
	        	int tpId = 1;
	        	int status = openStatusService.selectStatusById(tpId);
	            Page<TrainPlanVo> page = trainPlanService.selectTrainPlanPage(trainPlan,user, currentPage == null ? 1 : currentPage);
	            List<Depart> departList = departService.selectAllDepart();
	            List<Major> majorList = majorService.selectAllMajor();
	            // 列表项
	            mav.addObject("page", page.getResultList());
	            mav.addObject("opstatus", status);
	            mav.addObject("departList", departList);
	            mav.addObject("majorList", majorList);
	            // 分页参数
	            initPageParams(mav, page);
	            // 搜索条件
	            mav.addObject("trainPlan", trainPlan);
	            mav.addObject("trainPlanList",page.getResultList());
	            mav.setViewName("trainPlan/manager");
	        }catch (Exception e){
	            mav.setViewName("error/500");
	            e.printStackTrace();
	        }
	        return mav;
	    }
	 
	 /**
	  * 
	  * 批量删除
	  * @return
	  */
    @RequestMapping("trainPlanDeleteIDs")
    @ResponseBody
    public Result deleteIDs(String ids) {
    	try {
    		trainPlanService.deleteAll(ids.split(","));
			return new Result(true);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false);
		}
    }

    @RequestMapping("trainPlanDetail")
  	public ModelAndView trainPlanDetail(Integer id) {
  		ModelAndView mav = new ModelAndView();
  		try {
  			TrainPlanVo trainPlan = trainPlanService.selectVoByPrimaryKey(id);
  			List<TrainDetailVo> trainDetailList =  trainDetailService.selectVoByTrainPlanKey(id);
  			mav.addObject("trainPlan", trainPlan);
  			mav.addObject("trainDetailList", trainDetailList);
  			mav.setViewName("trainPlan/trainPlanDetail");
  		} catch (Exception e) {
  			e.printStackTrace();
  			mav.setViewName("common/error");
  			mav.addObject("error", "有异常！");
  		}
  		return mav;
  	}
    
    @RequestMapping("trainPlanCheckYes")
   	@ResponseBody
   	public Result trainPlanCheckYes(Integer id) {
   		try {
   			int yes = 1;
   			trainPlanService.updateStatus(id,yes);
   			return new Result(true);
   		} catch (Exception e) {
   			e.printStackTrace();
   			return new Result(false);
   		}
   	}
    
    @RequestMapping("trainPlanCheckNoUI")
	public ModelAndView trainPlanCheckNoUI(Integer id) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("id", id);
			mav.setViewName("trainPlan/checkNo");
		} catch (Exception e) {
			e.printStackTrace();
			mav.setViewName("common/error");
			mav.addObject("error", "有异常！");
		}
		return mav;
	}
    @RequestMapping("trainPlanCheckNoReason")
    public ModelAndView trainPlanCheckNoUI(String suggestion) {
    	ModelAndView mav = new ModelAndView();
    	try {
    		mav.addObject("suggestion", suggestion);
    		mav.setViewName("trainPlan/checkNoReason");
    	} catch (Exception e) {
    		e.printStackTrace();
    		mav.setViewName("common/error");
    		mav.addObject("error", "有异常！");
    	}
    	return mav;
    }

	@RequestMapping("trainPlanCheckNo")
	@ResponseBody
	public Result trainPlanCheckNo(int id,String suggestion) {
		try {
			int no = 2;
			trainPlanService.updateStatus(id,no);
			trainPlanService.updateSuggestion(id, suggestion);
			return new Result(true);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false);
		}
	}
	
	@RequestMapping("addUI")
	public ModelAndView trainPlanAddUI(Integer majorId) {
		ModelAndView mav = new ModelAndView();
		try {
			Major major = majorService.selectById(majorId);
			mav.addObject("major",major);
			mav.setViewName("/trainPlan/addUI");
		} catch (Exception e) {
			e.printStackTrace();
			mav.setViewName("common/error");
			mav.addObject("error", "有异常！");
		}
		return mav;
	}

	@RequestMapping("add")
	@ResponseBody
	public Result trainPlanAdd(Integer enterterm,String score1,String trainPlanName,Integer builderId,String status,String majorName,String majorCode,String term
			,String goal,String ability,String[] courseName,String[] courseCode,Double[] score,Double[] theory,Double[] experiment,
			Double[] selfmaster,String[] examtype,Integer[] termth) {
		try {
			Integer majorId = majorService.selectIdByMajorName(majorName);
			trainPlanService.add(enterterm,score1,trainPlanName,builderId,status,term,ability,goal,majorId);
			scheduleService.add(courseName,score,theory,experiment,selfmaster,examtype,termth);
			int latestTrainPlanId = trainPlanService.selectLatestTPId();
			List<Schedule> schedules = scheduleService.selectScheduleId(courseName.length);
			trainDetailService.add(latestTrainPlanId,schedules);
			return new Result(true);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false);
		}
	}
	
	 @RequestMapping("trainPlanDelete")
		@ResponseBody
		public Result trainPlanDelete(Integer id) {
			try {
				trainPlanService.deleteById(id);
				List<TrainDetailVo> tds = trainDetailService.selectVoByTrainPlanKey(id); 		
				scheduleService.deleteByTrainDetailId(tds);
				trainDetailService.deleteByIds(tds);
				return new Result(true);
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(false);
			}
		}
	 @RequestMapping("trainPlanUpdateUI")
		public ModelAndView trainPlanUpdateUI(Integer id) {
			ModelAndView mav = new ModelAndView();
			try {
				TrainPlanVo trainPlan = trainPlanService.selectVoByPrimaryKey(id);
	  			List<TrainDetailVo> trainDetailList =  trainDetailService.selectVoByTrainPlanKey(id);
	  			mav.addObject("trainPlan", trainPlan);
	  			mav.addObject("trainDetailList", trainDetailList);
				mav.setViewName("trainPlan/trainPlanUpdate");
			} catch (Exception e) {
				e.printStackTrace();
				mav.setViewName("common/error");
				mav.addObject("error", "有异常！");
			}
			return mav;
		}

		@RequestMapping("trainPlanUpdate")
		@ResponseBody
		public Result trainPlanUpdate(Integer trainPlanId,String term,String goal,String ability,String score1,String[] courseName,String[] courseCode,Double[] score,Double[] theory,Double[] experiment,
				Double[] selfmaster,String[] examtype,Integer[] termth,Integer[] scheduleId) {
			try {
				trainPlanService.edit(trainPlanId,term,goal,ability,score1,courseName,courseCode,score,theory,experiment,selfmaster,examtype,termth,scheduleId);
				return new Result(true);
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(false);
			}
		}
		
		@RequestMapping(value = "codeCheck", method = RequestMethod.POST)
	    @ResponseBody
		public Object codeCheck(String trainPlanName) {
			try {
				int count = trainPlanService.selectCount(trainPlanName.trim());
					if(count>0){
						return renderError("已存在");
					}
					return renderSuccess("");
			} catch (Exception e) {
				return renderError("方案名称出错");
			}

		} 
}
